Ext.define('Noter.view.NotetypeTree',{
    extend: 'Ext.tree.Panel',
    requires: [ 
            'Ext.data.TreeStore',
            'Noter.stores.NotetypesTree',
    ],
    alias: 'widget.notetypetree',

    viewConfig: {
        plugins: {
            ptype: 'treeviewdragdrop'
        },
    },                
    //width: navWidth-10,
    useArrows: true,
    dockedItems: [{
        xtype: 'toolbar',
        items: [{
            text: 'Expand All',
            handler: function(){
                tree.getEl().mask('Expanding tree...');
                var toolbar = this.up('toolbar');
                toolbar.disable();
                
                tree.expandAll(function() {
                    tree.getEl().unmask();
                    toolbar.enable();
                });
            }
        }, {
            text: 'Collapse All',
            handler: function(){
                var toolbar = this.up('toolbar');
                toolbar.disable();
                
                tree.collapseAll(function() {
                    toolbar.enable();
                });
            }
        }]
    }],

    treeContextMenu : null,

    listeners: {
        itemclick: function (v, r) {
            r.isExpanded() ? r.collapse() : r.expand();
        },
        itemcontextmenu: function(view, rec, node, index, e) {
            var me = this, menu = me.treeContextMenu;
            e.stopEvent();
            menu.showAt(e.getXY());
            menu.doConstrain();
        }
    },

    initComponent: function () {
        var me = this;
        var testAction = Ext.create('Ext.Action', {
            icon   : '../ext/examples/shared/icons/fam/delete.gif',  // Use a URL in the icon config
            text: 'Test',
            disabled: true,
            handler: function(widget, event) {
                var selection = me.getView().getSelectionModel().getSelection()[0];
                selection.data.ntname='xxxx';
                me.store.save(selection);
                console.log(selection.data.id);
                console.log(selection.data.ntname);
            }
        });

        me.treeContextMenu = Ext.create('Ext.menu.Menu', {
            items: [ testAction ]
        });            

        me.getSelectionModel().on('selectionchange', function(selModel, selections){
            if (selections.length) {
                testAction.enable();
            } else {
                testAction.disable();
            }                
        });
        
        this.callParent();
    },
 });
